package com.hqyj.javaSpringBoot.modules.test.controller;

import com.hqyj.javaSpringBoot.modules.common.entity.Result;
import com.hqyj.javaSpringBoot.modules.common.entity.SearchBean;
import com.hqyj.javaSpringBoot.modules.test.entity.Student;
import com.hqyj.javaSpringBoot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /*
     * 127.0.0.1/api/student ------post
     * {"name":"zuisheng","email":"zs@163.com","age":18}
     * {"name":"zuisheng1","email":"zs1@163.com","age":19,"studentCard":{"cardNo":"SFAC_00001"}}
     * */
    @PostMapping(value = "/student", consumes = "application/json")
    public Result<Student> insertStudent(@RequestBody Student student) {
        return studentService.editStudent(student);
    }

    /*
     * 127.0.0.1/api/student ------put
     * {"id":1,"name":"zuisheng","email":"zs@163.com","age":18}
     * {"id":1,"name":"zuisheng1","email":"zs1@163.com","age":19,"studentCard":{"cardNo":"SFAC_00001"}}
     * */
    @PutMapping(value = "/student", consumes = "application/json")
    public Result<Student> updateStudent(@RequestBody Student student) {
        return studentService.editStudent(student);
    }

    /*
    * 127.0.0.1/api/student/13  ----- delete
    * */
    @DeleteMapping(value = "/student/{id}")
    public Result<Object> deleteStudentById(@PathVariable int id) {
        return studentService.deleteStudentById(id);
    }

    /*
     * 127.0.0.1/api/student/2  ----- get
     * */
    @GetMapping(value = "/student/{id}")
    public Student getStudentById(@PathVariable int id){
        return studentService.getStudentById(id);
    }

    /*
    *  127.0.0.1/api/students  get
    * */
    @GetMapping(value = "/students")
    public List<Student> getStudents(){
        return studentService.getStudents();
    }

    /*
    * 127.0.0.1/api/student?name=zuisheng1&email=zs1@163.com -----get
    * */
    @GetMapping("/student")
    public Student getStudentByNameAndEmail(@RequestParam String name,
                                            @RequestParam String email){
        return studentService.getStudentByNameAndEmail(name,email);
    }

    /*
    * 127.0.0.1/api/students  ---post
    *{"currentPage":1,"pageSize":5,"orderBy":"name","direction":"desc","keyword":""}
    * */
    @PostMapping(value = "/students",consumes = "application/json")
    public Page<Student> getStudentsBySearchBean(@RequestBody SearchBean searchBean){
        return studentService.getStudentsBySearchBean(searchBean);
    }

    /*
    *127.0.0.1/api/students/v2 ------post
    * [{"name":"aa","email":"zs@163.com","age":18},
    * {"name":"aa1","email":"zs@163.com","age":18}]
    * */
    @PostMapping(value = "/students/v2", consumes = "application/json")
    public Result<Object> insertStudents(@RequestBody List<Student> students) {
        return studentService.insertStudents(students);
    }
}
